//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "ImpliedSpotHelper.h"
using namespace Cephei::QL::Experimental::Compoundoption;
#include <gen/QL/Instruments/PlainVanillaPayoff.h>
using namespace Cephei::QL::Instruments;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Compoundoption::CImpliedSpotHelper::CImpliedSpotHelper (Double dividendDiscount, Double riskFreeDiscount, Double standardDeviation, Cephei::QL::Instruments::IPlainVanillaPayoff^ payoff, Double strike) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CPlainVanillaPayoff^ _Cpayoff;
    try
    {
#ifdef HANDLE
        _phImpliedSpotHelper = NULL;
#endif
        QuantLib::DiscountFactor _dividendDiscount = (QuantLib::DiscountFactor)ValueHelper::Convert (dividendDiscount);
        QuantLib::DiscountFactor _riskFreeDiscount = (QuantLib::DiscountFactor)ValueHelper::Convert (riskFreeDiscount);
        QuantLib::Real _standardDeviation = (QuantLib::Real)ValueHelper::Convert (standardDeviation);
        _Cpayoff = safe_cast<CPlainVanillaPayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::PlainVanillaPayoff> _payoff = static_cast<boost::shared_ptr<QuantLib::PlainVanillaPayoff>> (_Cpayoff->GetShared ()); 
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike);
        _ppImpliedSpotHelper = new boost::shared_ptr<QuantLib::ImpliedSpotHelper> (new QuantLib::ImpliedSpotHelper ( _dividendDiscount,  _riskFreeDiscount,  _standardDeviation,  _payoff,  _strike ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
    }
}
Cephei::QL::Experimental::Compoundoption::CImpliedSpotHelper::CImpliedSpotHelper (boost::shared_ptr<QuantLib::ImpliedSpotHelper>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phImpliedSpotHelper = NULL;
#endif
	_ppImpliedSpotHelper = &childNative;
    
}
Cephei::QL::Experimental::Compoundoption::CImpliedSpotHelper::CImpliedSpotHelper (QuantLib::ImpliedSpotHelper& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phImpliedSpotHelper = NULL;
#endif
	_ppImpliedSpotHelper = new boost::shared_ptr<QuantLib::ImpliedSpotHelper> (&childNative);
    
    _ImpliedSpotHelperOwner = owner;
    
}

Cephei::QL::Experimental::Compoundoption::CImpliedSpotHelper::CImpliedSpotHelper (CImpliedSpotHelper^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phImpliedSpotHelper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppImpliedSpotHelper = new boost::shared_ptr<QuantLib::ImpliedSpotHelper> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Compoundoption::CImpliedSpotHelper::CImpliedSpotHelper (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phImpliedSpotHelper = NULL;
#endif
	if (!t->IsSubclassOf(CImpliedSpotHelper::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Compoundoption::CImpliedSpotHelper::CImpliedSpotHelper (QuantLib::Handle<QuantLib::ImpliedSpotHelper>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phImpliedSpotHelper = &childNative;
	_ppImpliedSpotHelper = &static_cast<boost::shared_ptr<QuantLib::ImpliedSpotHelper>>(childNative.currentLink());
    
    _ImpliedSpotHelperOwner = owner;
}
Cephei::QL::Experimental::Compoundoption::CImpliedSpotHelper::CImpliedSpotHelper (QuantLib::Handle<QuantLib::ImpliedSpotHelper> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phImpliedSpotHelper = &childNative;
	_ppImpliedSpotHelper = &static_cast<boost::shared_ptr<QuantLib::ImpliedSpotHelper>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Compoundoption::CImpliedSpotHelper::CImpliedSpotHelper (QuantLib::ImpliedSpotHelper childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phImpliedSpotHelper = NULL;
#endif
	_ppImpliedSpotHelper = new boost::shared_ptr<QuantLib::ImpliedSpotHelper> (new QuantLib::ImpliedSpotHelper (childNative));
    
}
#endif

Cephei::QL::Experimental::Compoundoption::CImpliedSpotHelper::~CImpliedSpotHelper ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppImpliedSpotHelper != NULL)
    {
	    delete _ppImpliedSpotHelper;
        _ppImpliedSpotHelper = NULL;
    }
}
Cephei::QL::Experimental::Compoundoption::CImpliedSpotHelper::!CImpliedSpotHelper ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppImpliedSpotHelper != NULL)
    {
	    delete _ppImpliedSpotHelper;
    }
}
QuantLib::ImpliedSpotHelper& Cephei::QL::Experimental::Compoundoption::CImpliedSpotHelper::GetReference ()
{
    if (_ppImpliedSpotHelper == NULL) throw gcnew NativeNullException ();
	return **_ppImpliedSpotHelper;
}
boost::shared_ptr<QuantLib::ImpliedSpotHelper>& Cephei::QL::Experimental::Compoundoption::CImpliedSpotHelper::GetShared ()
{
    if (_ppImpliedSpotHelper == NULL) throw gcnew NativeNullException ();
	return *_ppImpliedSpotHelper;
}
QuantLib::ImpliedSpotHelper* Cephei::QL::Experimental::Compoundoption::CImpliedSpotHelper::GetPointer ()
{
    if (_ppImpliedSpotHelper == NULL) throw gcnew NativeNullException ();
	return &**_ppImpliedSpotHelper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ImpliedSpotHelper>& Cephei::QL::Experimental::Compoundoption::CImpliedSpotHelper::GetHandle ()
{
	if (_phImpliedSpotHelper == NULL)
	{
		_phImpliedSpotHelper = new Handle<QuantLib::ImpliedSpotHelper> (*_ppImpliedSpotHelper);
	}
	return *_phImpliedSpotHelper;
}
#endif
bool Cephei::QL::Experimental::Compoundoption::CImpliedSpotHelper::HasNative () 
{
	return (_ppImpliedSpotHelper != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Compoundoption::IImpliedSpotHelper^ Cephei::QL::Experimental::Compoundoption::CImpliedSpotHelper_Factory::Create (Double dividendDiscount, Double riskFreeDiscount, Double standardDeviation, Cephei::QL::Instruments::IPlainVanillaPayoff^ payoff, Double strike)
{
    return gcnew CImpliedSpotHelper ( dividendDiscount,  riskFreeDiscount,  standardDeviation,  payoff,  strike);
}
